import javax.swing.JOptionPane;

/*
 * Josh Lindoo
 * Login ID : lind6441
 * CS-102, Winter 2013
 * Programming Assignment 5
 * Showing class: Stores information about movie showings
 */

public class Showing implements Comparable<Showing>{
	
	//Fields
	private int numericalID = 0; //unique ID of corresponding movie
	private String title = null; //title of corresponding movie
	private int dateInt = 0; //date of showing
	private int showTimeInt = 0; //time of showing
	private String dateStr = null; //str of date
	private String showTimeStr = null; //str of time
	
	//Constructor
	public Showing (String[] showingData) {
		//Store data in respective fields
		this.dateStr = showingData[1];
		this.showTimeStr = showingData[2];
		
		try { //check inputs
			this.numericalID = Integer.parseInt(showingData[0]);
			this.dateInt = Integer.parseInt(showingData[1]); 
			this.showTimeInt = Integer.parseInt(showingData[2]);
		}
		catch(NumberFormatException exc) {
			JOptionPane.showMessageDialog(null, "Show Error: Input " +
					"contains non-integer characters. Exiting program..");
			System.exit(0);
		}
	}
	
	
	/*
	 * Method: export
	 * Purpose: outputs the showing data in importable format
	 * Parameters: none
	 * Returns: ID/Title/Run Time/Rating
	 */
	
	public String export() {
		return "" + numericalID + "/" + dateStr + "/" + showTimeStr;
	}
	
	/*
	 * Method: formatDate
	 * Purpose: Convert date input into readable format
	 * Parameters:
	 * 		String: unformatted		stores raw date data
	 * Returns: str		Readable date data
	 */
	
	private String formatDate(String unformatted) {
		//if the date is the correct length
		if (unformatted.length() == 8) {
			//split the input using substrings (yyyymmdd)
			String year = unformatted.substring(0,4);
			String month = unformatted.substring(4,6);
			String day = unformatted.substring(6,8);
			
			//return a string in dd/mm/yyyy format
			return day + "/" + month + "/" + year;
		}
		//else input is incorrect
		else {
			System.out.println("Error: Found invalid showing date.");
			System.exit(0);
		}
		
		return "";
	}
	
	/*
	 * Method: formatTime
	 * Purpose: Convert time input into readable format
	 * Parameters:
	 * 		String: unformatted		stores raw time data
	 * Returns: str		Readable time data
	 */
	
	private String formatTime(String unformatted) {
		int hours = 0;
		String minutes = null;
		//if time is the correct length
		if (unformatted.length() == 4) {
			//split the input using substrings
			hours = Integer.parseInt(unformatted.substring(0,2));
			minutes = unformatted.substring(2,4);
			
			//if the hours are less than 12 use am
			if (hours < 12)
				return hours + ":" + minutes + "am";
			//else return hours -12 and use pm
			else if (hours == 12)
				return hours + ":" + minutes + "pm";
			else
				return (hours - 12) + ":" + minutes + "pm";
		}
		//else input is incorrect
		else {
			System.out.println("Error: Found invalid showing time.");
			System.exit(0);
		}
		
		return "";
	}
	
	/*
	 * Method: setTitle
	 * Purpose: Mutator method for showing's movie title
	 * Parameters:
	 * 		String: newTitle	The new title for the showing
	 * Returns: void
	 */
	
	public void setTitle(String newTitle) {
		title = newTitle;
	}
	
	/*
	 * Methods: getID, getTitle, getDate
	 * Purpose: Accessor methods for the unique ID, title, date, and
	 * 			fullTime
	 * Parameters: none
	 * getID Returns: int		unique ID of showing/corresponding movie
	 * getTitle Returns: str	title of movie to be shown
	 * getDate Returns: int		date
	 * getFullTime Returns: long	date and time combined for comparison
	 */
	
	public int getID() {
		return numericalID;
	}
	
	public String getTitle() {
		return title;
	}
	
	public int getDate() {
		return dateInt;
	}
	
	public String getFullTime() {
		String datestring = dateStr + showTimeStr; //combine date and time strs
		return datestring;
	}
	
	/*
	 * Method: toString
	 * Purpose: Return showing data in string format
	 * Parameters:
	 * Returns: str		Readable showing data
	 */
	
	public String toString() {
		return formatDate(dateStr) + ": " + title + ", " +
							formatTime(showTimeStr);
	}

	//TODO//Override compareTo
	public int compareTo(Showing show) {
		return this.getFullTime().compareTo(show.getFullTime());
	}

}
